<template>
  <div>
    <p class="pinformation" style="margin-top: 7px">搜索条件</p>
    <el-form label-width="100px" :model="queryParams" ref="queryRef">
      <el-row>
        <el-col :span="4">
          <el-form-item label="方案编号：" prop="projectCode">
            <el-input v-model="queryParams.projectCode" clearable maxlength="100" @keyup.enter="handleQuery"
              placeholder="请输入方案编号"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="委托单位：" prop="wtCustomName" label-width="75">
            <el-input v-model="queryParams.wtCustomName" clearable maxlength="100" @keyup.enter="handleQuery"
              placeholder="请输入委托单位"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="方案名称：" prop="projectName">
            <el-input v-model="queryParams.projectName" clearable maxlength="100" @keyup.enter="handleQuery"
              placeholder="请输入方案名称"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="样品编号：" prop="sampleCode">
            <el-input v-model="queryParams.sampleCode" clearable maxlength="100" @keyup.enter="handleQuery"
              placeholder="请输入样品编号"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="检测项目：" prop="jcxmName" label-width="80">
            <el-input v-model="queryParams.jcxmName" clearable maxlength="100" @keyup.enter="handleQuery"
              placeholder="请输入检测项目"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="方法编号：" prop="methodCode">
            <el-input v-model="queryParams.methodCode" clearable maxlength="100" @keyup.enter="handleQuery"
              placeholder="请输入方法编号"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="5">
          <!-- planDate -->
          <el-form-item label="采样时间：">
            <el-date-picker v-model="dateRange" value-format="YYYY-MM-DD" type="daterange" :clearable="false"
              :editable="false" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="3">
          <el-form-item label="当前状态：" prop="a" label-width="75">
            <el-select placeholder="请选择" clearable v-model="queryParams.a" filterable>
              <el-option v-for="dict in proxy.addListNoLimit(sample_handle_status)" :key="dict.value" :label="dict.label"
                :value="parseInt(dict.value)" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="交接批次：" prop="withNumber">
            <el-input v-model="queryParams.withNumber" clearable maxlength="100" @keyup.enter="handleQuery"
              placeholder="请输入交接批次"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="检测类别：" prop="jclbId">
            <el-select style="width: 100%" clearable filterable v-model="queryParams.jclbId" placeholder="选择检测类别">
              <el-option v-for="item in proxy.addListNoLimit(
                jclbList,
                'jclbId',
                'jclbName'
              )" :key="item.jclbId" :value="item.jclbId" :label="item.jclbName" />
            </el-select>
          </el-form-item>
        </el-col>
        <!-- ！ -->
        <el-col :span="5">
          <el-button type="success" style="margin-left: 30px" @click="handleQuery">搜索</el-button>
          <el-button class="business resetBtn" @click="resetQuery">重置</el-button>
        </el-col>
      </el-row>
    </el-form>
    <el-row>
      <p class="pinformation" style="margin-top: 8.5px">样品列表</p>
      <p class="totalStyle" style="margin-top: 6px">共 {{ total }} 条数据</p>
      <el-row style="padding-bottom: 5px; margin-left: 25px">
        <el-button link type="success">扫码处置</el-button>
        <el-form-item label="扫码结果：" style="margin-bottom: 0px !important; margin-left: 15px">
          <el-input maxlength="100" clearable placeholder="请输入扫码结果" />
        </el-form-item>
        <el-button link type="success" style="margin-left: 25px" :disabled="!sFIds || sFIds.length == 0" @click="useCz()">批量处置</el-button>
      </el-row>
    </el-row>

    <el-table ref="mainTableRef" :data="sampleList" v-loading="loading" row-key="sFId"
      @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" :reserve-selection="true" />
      <el-table-column show-overflow-tooltip label="样品编号" align="center" prop="sampleCode" width="160" />
      <el-table-column show-overflow-tooltip label="检测项目" align="center" prop="jcxmNames">
        <template #default="scope">
          <span v-html="proxy.getSupAndSub(scope.row.jcxmNames)"></span>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip label="方案编号" align="center" prop="projectCode" width="150" />
      <el-table-column show-overflow-tooltip label="方案名称" align="center" prop="projectName" />
      <el-table-column show-overflow-tooltip label="检测点位" align="center" prop="pointName" width="110" />
      <el-table-column show-overflow-tooltip label="频次" align="center" prop="sampleCount" width="130">
        <template #default="scope">
          <span v-if="scope.row.sampleDay">第{{ scope.row.sampleDay }}天，第{{
            scope.row.sampleCount
          }}次</span>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip label="采样时间" align="center" prop="cyDate" width="140">
        <template #default="scope">{{ scope.row.cyDate }} {{ scope.row.cyTime }}</template>
      </el-table-column>
      <el-table-column show-overflow-tooltip label="保存时长" align="center" prop="saveTime" width="140">
        <template #default="scope">{{ scope.row.saveTime }}{{ scope.row.timeUnitName }}</template>
      </el-table-column>
      <el-table-column show-overflow-tooltip label="保存方式" align="center" prop="saveDesc" />
      <el-table-column show-overflow-tooltip label="交接批次" align="center" prop="withNumber" width="80" />
      <el-table-column show-overflow-tooltip label="检测类别" align="center" prop="jclbName" width="110" />
      <el-table-column show-overflow-tooltip label="存放位置" align="center" />
      <el-table-column show-overflow-tooltip label="当前状态" align="center" width="110" />
      <el-table-column show-overflow-tooltip label="操作" align="center" width="120">
        <template #default="scope">
          <el-button link type="success" @click="useCz(scope.row.sFId)">处置</el-button>
          <!-- <el-button link type="success">领取</el-button> -->
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
      @pagination="getList" />

    <el-dialog :class="'el-dialog-theme3'" append-to-body draggable :close-on-click-modal="false"
      v-model="useOpen" @close="closedDialog" title="样品处置" width="400">
      <el-form :model="lyForm" ref="lyRef" :rules="lyRules">
        <el-form-item label="已选中标样编号数：" prop="">{{
          lyForm.codeCount
        }}</el-form-item>
        <el-form-item label="已选中检测项目数：" prop="">{{
          lyForm.jcxmCount
        }}</el-form-item>
        <el-form-item label="处置方式：" prop="handleType" label-width="80">
          <el-select placeholder="请选择" clearable v-model="lyForm.handleType" filterable>
            <el-option v-for="dict in sample_handle_type" :key="dict.value" :label="dict.label"
              :value="parseInt(dict.value)" />
          </el-select>
        </el-form-item>
        <el-form-item label="处置原因：" prop="handleReason" label-width="80">
          <el-select placeholder="请选择" clearable v-model="lyForm.handleReason" filterable>
            <el-option v-for="dict in sample_handle_reason" :key="dict.value" :label="dict.label"
              :value="parseInt(dict.value)" />
          </el-select>
        </el-form-item>
        <el-form-item label="处置日期：" prop="handleDate" label-width="80">
          <el-date-picker placeholder="请选择处置日期" type="date" v-model="lyForm.handleDate" value-format="YYYY-MM-DD" />
        </el-form-item>
        <el-form-item label="留样到期日期：" prop="lyDate" label-width="110" v-if="lyForm.handleType == 5">
          <el-date-picker placeholder="请选择留样到期日期" type="date" v-model="lyForm.lyDate" value-format="YYYY-MM-DD" />
        </el-form-item>
        <el-form-item label="留样位置：" prop="lyLocation" label-width="80" v-if="lyForm.handleType == 5">
          <el-input clearable maxlength="100" v-model="lyForm.lyLocation"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="success" @click="save">保 存</el-button>
          <el-button type="danger" @click="useOpen = false">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import {
  getNotDisposedList,
  handleForm,
  saveBatchHandle,
} from "@/api/sampleManage/retention";
import { getCurrentTime } from "@/api/common";
import { allListJclb } from "@/api/store/jclb";
const { proxy } = getCurrentInstance();
const queryParams = ref({ pageNum: 1, pageSize: 10 });
const lyForm = ref({});
const dateRange = ref([]);
const sampleList = ref([]);
const total = ref(0);
const useOpen = ref(false);
const sFIds = ref([]);
const loading = ref(false);
const lyRules = ref({
  handleType: [
    { required: true, message: "处置方式不能为空", trigger: "blur" },
  ],
  handleDate: [
    { required: true, message: "处置日期不能为空", trigger: "blur" },
  ],
  lyDate: [
    { required: true, message: "留样到期日期不能为空", trigger: "blur" },
  ],
  handleReason: [
    { required: true, message: "处置原因不能为空", trigger: "blur" },
  ],
});
const { sample_handle_type, sample_handle_reason, sample_handle_status } = proxy.useDict(
  "sample_handle_type",
  "sample_handle_reason",
  "sample_handle_status"
);

function getList() {
  loading.value = true;
  getNotDisposedList(
    proxy.addDateRange(queryParams.value, dateRange.value)
  ).then((res) => {
    sampleList.value = res.rows;
    total.value = res.total;
    loading.value = false;
  });
}

getList();

function handleSelectionChange(selection) {
  sFIds.value = selection.map((item) => {
    return item.sFId;
  });
}

function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

function resetQuery() {
  proxy.resetForm("queryRef");
  dateRange.value = [];
  handleQuery();
}

function useCz(id) {
  const ids = id ? [id] : sFIds.value
  handleForm(ids).then((res) => {
    lyForm.value = res.data;
    getCurrentTime().then((res) => {
      lyForm.value.handleDate = res.data;
    });
    lyForm.value.handleType = 1;
  });
  useOpen.value = true;
}

function save() {
  proxy.$refs["lyRef"].validate((valid) => {
    if (valid) {
      saveBatchHandle(lyForm.value).then((res) => {
        proxy.$modal.msgSuccess(res.msg);
        useOpen.value = false;
        getList();
      });
    }
  });
}

function closedDialog() {
  lyForm.value = {};
  proxy.$refs["lyRef"]?.clearValidate();
}

defineExpose({
  getList,
});

const heightParams = ref([{ tableName: "main", bottomHeight: 21 }]);
function getTableHeight() {
  proxy.adaptiveHeight(proxy, heightParams.value);
}

onMounted(() => {
  nextTick(function () {
    getTableHeight();
    window.addEventListener("resize", getTableHeight);
  });
});

onUpdated(() => {
  nextTick(function () {
    getTableHeight();
    window.addEventListener("resize", getTableHeight);
  });
});
onBeforeUnmount(() => {
  window.removeEventListener("resize", getTableHeight);
});

watch(
  () => total.value,
  () => {
    if (total.value) {
      heightParams.value = [{ tableName: "main", bottomHeight: 73 }];
    } else {
      heightParams.value = [{ tableName: "main", bottomHeight: 21 }];
    }
    getTableHeight();
  }
);

const jclbList = ref([]);
allListJclb().then((res) => {
  jclbList.value = res.data;
});
</script>
